package cn.dankal.flyaudio.personal

import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory
import api.UserServiceFactory
import cn.dankal.basiclib.DKUserManager
import cn.dankal.basiclib.DankalApplication
import cn.dankal.basiclib.GlideApp
import cn.dankal.basiclib.base.fragment.BaseFragment
import cn.dankal.basiclib.pojo.UserInfoBean
import cn.dankal.basiclib.rx.AbstractDialogSubscriber
import cn.dankal.basiclib.util.image.PicUtils
import cn.dankal.business.eventbus.MessageUpdatePersonEvent
import cn.dankal.flyaudio.R
import cn.dankal.flyaudio.personal.driverlisense.DriverLisenseActivity
import cn.dankal.flyaudio.personal.notificaiton.NotificationActivity
import cn.dankal.flyaudio.personal.setting.SettingActivity
import com.bumptech.glide.request.target.BitmapImageViewTarget
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.fragement_personal.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


class PersonalFragment : BaseFragment() {
    companion object {
        fun newInstance(): PersonalFragment {
            val fragment = PersonalFragment()
            return fragment
        }
    }

    var isLoding = false
    var isHead = false


    override fun initComponents() {

        iv_notify.setOnClickListener {
            startActivity(Intent(context, NotificationActivity::class.java))

        }
        tv_baseinfo.setOnClickListener {
            startActivity(Intent(context, BaseInfoActivity::class.java))
        }
        tv_driver_identify.setOnClickListener {
            startActivity(Intent(context, DriverLisenseActivity::class.java))
        }
        tv_my_activity.setOnClickListener {
            startActivity(Intent(context, MyActivityActivity::class.java))
        }
        tv_my_current.setOnClickListener {
            startActivity(Intent(context, MyCurrentActivity::class.java))

        }
        tv_setting.setOnClickListener {
            startActivity(Intent(context, SettingActivity::class.java))

        }
        swipe_toload_layout.setOnRefreshListener {
            getUserInfo()
        }
        swipe_toload_layout.isLoadMoreEnabled = false
    }

    override fun obtainData() {
        super.obtainData()
        getUserInfo()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        EventBus.getDefault().register(this)
    }


    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: MessageUpdatePersonEvent) {
        isLoding = true
        if (event.path != null) {
            isHead = true
            GlideApp.with(DankalApplication.getContext()).asBitmap().centerCrop().error(R.mipmap.pic_ill_4).load(event.path).into(object : BitmapImageViewTarget(iv_user) {
                override fun setResource(resource: Bitmap?) {
                    val circularBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), resource)
                    circularBitmapDrawable.isCircular = true
                    iv_user.setImageDrawable(circularBitmapDrawable)
                }
            })
            //PicUtils.loadUriAvatar(event.uri, iv_user)
        }
    }

    override fun onResume() {
        super.onResume()
        if (isLoding)
            getUserInfo()
    }

    private fun getUserInfo() {
        if (DKUserManager.isLogined()) {
            UserServiceFactory.user_info()
                    .subscribe(object : AbstractDialogSubscriber<UserInfoBean>(this) {
                        override fun onNext(userInfo: UserInfoBean) {
                            DKUserManager.updateUserInfo(userInfo)
                            tv_level.setText(if (userInfo.isTourist()) "游客" else "会员")
                            iv_level.setImageResource(if (userInfo.isTourist())
                                R.mipmap.ic_mine_vip_formal else R.mipmap.ic_mine_vip)

                            tv_name.setText(userInfo.name)
                            if (!isHead) {
                                PicUtils.loadAvatar(userInfo.avatar, iv_user)
                            }
                            swipe_toload_layout.isRefreshing = false
                            isCreate = false
                            isHasLoadOnce = true
                            isLoding = false
                        }

                        override fun onSubscribe(d: Disposable) {

                        }
                    })
        }
    }


    override fun getLayoutId(): Int {
        return R.layout.fragement_personal
    }
}